package net.lwf.handler.wp.service;

import java.util.ArrayList;
import java.util.List;

import net.lwf.handler.constant.IHandlerConstant;
import net.lwf.hibernate.dao.WordProblemDAO;
import net.lwf.hibernate.entity.wp.WpData;
import net.lwf.number.util.RandomNumberGenerator;
import net.lwf.service.LwfService;
import net.lwf.wp.fake.jaxb.Data;
import net.lwf.wp.fake.service.MockWpDataService;

public class WordProblemService {
	
	public WpData getSingleData(String grade, String type) {
		WpData wpdata = null;
		if(LwfService.isMock()) {
			wpdata = this.getMockSingleData(grade, type);
		}
		else {
			List<WpData> data = this.getActualData(grade, type);
			if(data != null) {
				int pick = RandomNumberGenerator.generate(1, data.size());
				wpdata = data.get(pick-1);
			}
		}
		return wpdata;
	}
	
	public List<WpData> getMultipleData(String grade, String type) {
		List<WpData> data = null;
		if(LwfService.isMock()) {
			data = this.getMockMultipleData(grade, type);
		}
		else {
			data = this.getActualData(grade, type);
		}
		if(data != null) {
			this.resetWpData(data);
		}
		return data;
	}
	
	private void resetWpData(List<WpData> data) {
		int actualneeded = IHandlerConstant.WORD_PROBLEMS_FOR_PDF_GENERATION;
		int available = data.size();
		int needed = 0;
		if(available < actualneeded) {
			needed = actualneeded - available;
			for(int i=0; i<needed; i++) {
				int random = RandomNumberGenerator.generate(1, available);
				data.add(data.get(random-1));
			}
		}
	}
	
	private List<WpData> getActualData(String grade, String type) {
		return new WordProblemDAO().getWordProblems(grade, type);
	}
	//---------------------------------------------------------------------------------------------
	// Mock Data retrieval methods
	//---------------------------------------------------------------------------------------------
	private WpData getMockSingleData(String grade, String type) {
		WpData wpdata = null;
		List<Data> mockdata = this.getMockService().getMockData(grade, type);
		if(mockdata != null) {
			int pick = RandomNumberGenerator.generate(1, mockdata.size());
			Data data = mockdata.get(pick-1);
			 wpdata = toWpData(grade, type, data);
		}
		else {
			//TODO throw Exception
		}
		return wpdata;
	}
	
	private List<WpData> getMockMultipleData(String grade, String type) {
		List<WpData> wpdata = null;
		List<Data> mockdata = this.getMockService().getMockData(grade, type);
		if(mockdata != null && mockdata.size() != 0) {
			wpdata = new ArrayList<WpData>();
			for(Data data: mockdata) {
				wpdata.add(this.toWpData(grade, type, data));
			}
		}

		return wpdata;
	}

	private WpData toWpData(String grade, String type, Data data) {
		WpData wpdata;
		wpdata = new WpData();
		 wpdata.setGrade(grade);
		 wpdata.setType(type);
		 wpdata.setId(Long.parseLong(data.getId()));
		 wpdata.setClassName(data.getClassName());
		 wpdata.setProblem(data.getProblem());
		 wpdata.setReplacements(data.getReplacements());
		 wpdata.setDataInput(data.getInput());
		 wpdata.setRemarks(data.getRemarks());
		return wpdata;
	}
	
	private MockWpDataService getMockService() {
		return new MockWpDataService();
	}

}
